import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86131
 * Date: 2023-12-10
 * Time: 21:15
 */

//端口扫描
public class DDScan {
    public void doAllScan(String ip,int startPort,int endPort,int timeout){  //IP 开始端口 结束端口 时间
        for (int port = startPort;port<endPort;port++){
            Socket socket = new Socket();
            InetSocketAddress socketAddress = new InetSocketAddress(ip, port);
            try {
                socket.connect(socketAddress,timeout);
                socket.close();
                System.out.println("端口"+ port+",开放");
            } catch (IOException e) {
                //throw new RuntimeException(e);
                //System.out.println("端口"+port+",关闭");
            }
        }
    }

    public static void main(String[] args) {
        DDScan ds = new DDScan();
        ds.doAllScan("192.168.20.136",1,5000,30);
    }
}
